package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.junzhiit.nengji.bo.RebateInfoBO;
import com.junzhiit.nengji.dao.RebateInfoDAO;
import com.junzhiit.nengji.model.RebateInfo;

@Repository
public class RebateInfoDAOImpl extends AbstractBaseDAO<RebateInfo> implements RebateInfoDAO {

	@Override
	public int getRebatInfoEndCode(String rebateCategoryId) {
		String sql = "select ifnull(max(end_code),1) from rebate_info where rebate_category_id =?";
		StatementParameter params = new StatementParameter();
		params.setString(rebateCategoryId);
		return super.queryForInt(sql,params);
	}

	@Override
	public RebateInfo getRebatInfo(String rebateCategoryId, Integer userCode) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from rebate_info");
		sql.append("  where rebate_category_id = ? and ? between start_code and end_code");
		StatementParameter params = new StatementParameter();
		params.setString(rebateCategoryId);
		params.setInt(userCode);
		return super.query(sql.toString(),RebateInfo.class,params);
	}

	@Override
	public List<RebateInfoBO> getRebatInfoByUserId(String userId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from rebate_info");
		sql.append(" where user_id = ?");
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		sql.append(" order by created_time");
		return super.queryForList(sql.toString(),RebateInfoBO.class, params);
	}

	@Override
	public List<RebateInfoBO> updateRebate() {
		StringBuffer sql = new StringBuffer();
		sql.append("select o.pay_time,o.id as orderId,o.`status`,g.id as goodsId,g.price as goodsPrice,o.pay_money,g.shop_id");
		sql.append(" ,o.user_id,g.`name` as goodsName,og.number,g.is_open_rebate,sc.price as skuPrice,og.is_open_sku,og.id as orderGoodsId");
		sql.append(" from orders_goods og");
		sql.append(" inner join orders o on o.id = og.order_id");
		sql.append(" inner join goods g on g.id = og.goods_id ");
		sql.append(" left join sku_content sc on sc.id = og.sku_content_id");
		sql.append(" where g.is_open_rebate = 1 and o.`status` not in(0,7,8,9) ORDER BY o.pay_time ");
		return super.queryForList(sql.toString(),RebateInfoBO.class );
	}

}
